#ifndef B2D_UTILITIES_H
#define B2D_UTILITIES_H

#include "IUncopyable.h"

NS_B2D_BEGIN

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \brief	Sets the object to null upon construction and restores its value on destruction.
/// 		ie. this:
/// 		{
/// 			auto tmp = mObject;
/// 			mObject = nullptr;
/// 			do_something();
/// 			mObject = tmp;
/// 		}
/// 		becomes:
/// 		{
/// 			TempNullify<T> tmpnull(mObject);
/// 			do_something();
/// 		}
/// 		If do_something() throws, the value will still be recovered.
///
/// \tparam	T	Type of object.
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename T>
class B2D_API TempNullify final
	: private IUncopyableNonVirtual
{
private:
	T*& mTarget;
	T* mValue;

public:
	TempNullify(T*& target)
		: mTarget(target)
		, mValue(target)
	{
		mTarget = nullptr;
	}
	~TempNullify(void)
	{
		mTarget = mValue;
	}
};


NS_B2D_END

#endif
